package com.han.verifycode.ver.exact;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.han.verifycode.CodeConfig;
import com.han.verifycode.data.ArrayData;
import com.han.verifycode.data.FullPath;
import com.han.verifycode.data.QuestionPair;
import com.han.verifycode.data.RightKey;
import com.han.verifycode.enums.VerifyCodeEnum;
import com.han.verifycode.utils.DrawCodeUtil;
import com.han.verifycode.utils.FileUtil;
import com.han.verifycode.utils.RandomUtil;
import com.han.verifycode.utils.StrUtil;
import com.han.verifycode.ver.ExactMatchCode;
import com.han.verifycode.ver.conf.FixKeyPairConf;

import java.io.*;
import java.util.List;

/**
 * 固定答案
 * 需要提供外部数据源
 */
public class FixedKeyPairCode implements ExactMatchCode {
    private VerifyCodeEnum type = VerifyCodeEnum.FixedKeyPair;
    private FixKeyPairConf thisConf;

    @Override
    public RightKey generate(String code, CodeConfig conf) {
        RightKey key = new RightKey(code);
        if (conf == null) {
            thisConf = new FixKeyPairConf();
        } else {
            thisConf = (FixKeyPairConf) conf;
        }
        key.setConfig(thisConf);
        key.setType(type);
        key.setTips("请回答图片中的问题");
        key.setWidth(conf.getWidth());
        key.setHeight(conf.getHeight());
        String dataSource = thisConf.getDataSource();
        if (StrUtil.isEmpty(dataSource)) {
            throw new RuntimeException("未指定数据源");
        }
        QuestionPair qp = getRandomQuestion(dataSource);
        key.setAnswer(qp.getA());
        key.setDesc(qp.getQ());
        FullPath fullPath = generateFilePath(thisConf.getBasePath(), key.getCode());
        key.setFullPath(fullPath);
        draw(key);
        return key;
    }


    /**
     * 获取随机问题
     *
     * @param file
     * @return
     */
    private QuestionPair getRandomQuestion(String file) {
        InputStream input = FileUtil.getFile(file);
        ObjectMapper mapper = StrUtil.getMapper();
        ArrayData re = null;
        try {
            re = mapper.readValue(input, new TypeReference<ArrayData<QuestionPair>>() {
            });
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("数据源格式错误");
        }
        List<QuestionPair> s = re.getArrays();
        int index = RandomUtil.getRandom().nextInt(s.size() - 1);

        return s.get(index);
    }
    /**
     * 调用画图
     *
     * @param key
     */
    private void draw( RightKey key) {
        String filePath=key.getFullPath().getAbsolutePath();
        FileOutputStream f = null;
        try {
            f = new FileOutputStream(new File(filePath));
            DrawCodeUtil.outputImage(thisConf.getWidth().intValue(), thisConf.getHeight().intValue(), f, key.getDesc(), thisConf.isSalt(), false);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (f != null) {
                try {
                    f.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public CodeConfig getConfig() {
        return thisConf;
    }
}
